﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace Draco.Commons.Lang
{
    /// <summary>
    /// 时间相关辅助方法
    /// </summary>
    public class DateTimeHelper
    {
        /// <summary>
        /// 取得短日期格式yyyy-MM-dd
        /// </summary>
        /// <param name="strDate"></param>
        /// <returns></returns>
        public static string GetShortDateString(string strDate)
        {
            if (String.IsNullOrEmpty(strDate))
                return "0000-00-00";
            try
            {
                return Convert.ToDateTime(strDate, NumberFormatInfo.CurrentInfo).
                    ToString("yyyy-MM-dd", NumberFormatInfo.CurrentInfo);
            }
            catch (Exception)
            { return "0000-00-00"; }
        }
        /// <summary>
        /// 取得短日期格式(yyyy-MM-dd)
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string GetShortDateString(DateTime date)
        {
            return date.ToString("yyyy-MM-dd", NumberFormatInfo.CurrentInfo);
        }
        /// <summary>
        /// 获取短时间字符串(HH:mm:ss)
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string GetShortTimeString(DateTime date)
        {
            return date.ToString("HH:mm:ss", NumberFormatInfo.CurrentInfo);
        }
        /// <summary>
        /// 取得短时间格式HH:mm:ss
        /// </summary>
        /// <param name="strDate"></param>
        /// <returns></returns>
        public static string GetShortTimeString(string strDate)
        {
            if (String.IsNullOrEmpty(strDate))
                return "00:00:00";
            try
            {
                return Convert.ToDateTime(strDate, NumberFormatInfo.CurrentInfo).
                    ToString("HH:mm:ss", NumberFormatInfo.CurrentInfo);
            }
            catch (Exception)
            { return "00:00:00"; }
        }

        /// <summary>
        /// 提取DateTime中的日期(2011-08-30 09:05:10  转换为2011-08-30 00:00:00)
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static DateTime GetDate(DateTime datetime)
        {
            String str = datetime.ToString("yyyy-MM-dd");
            return DateTime.Parse(str + " 00:00:00");
        }

        /// <summary>
        /// 把日期中的day(0~31)转换为中文数字,用于日期型
        /// </summary>
        /// <param name="Num"></param>
        /// <returns></returns>
        public static string ConvertDayToUpper(int day)
        {
            if (day < 0 || day > 32)
                throw new ArgumentException("day 必须介于0与31之间");
            string strContent = "";
            switch (day)
            {
                case 0:
                    strContent = "〇"; break;
                case 1:
                    strContent = "一"; break;
                case 2:
                    strContent = "二"; break;
                case 3:
                    strContent = "三"; break;
                case 4:
                    strContent = "四"; break;
                case 5:
                    strContent = "五"; break;
                case 6:
                    strContent = "六"; break;
                case 7:
                    strContent = "七"; break;
                case 8:
                    strContent = "八"; break;
                case 9:
                    strContent = "九"; break;
                case 10:
                    strContent = "十"; break;
                case 11:
                    strContent = "十一"; break;
                case 12:
                    strContent = "十二"; break;
                case 13:
                    strContent = "十三"; break;
                case 14:
                    strContent = "十四"; break;
                case 15:
                    strContent = "十五"; break;
                case 16:
                    strContent = "十六"; break;
                case 17:
                    strContent = "十七"; break;
                case 18:
                    strContent = "十八"; break;
                case 19:
                    strContent = "十九"; break;
                case 20:
                    strContent = "二十"; break;
                case 21:
                    strContent = "二十一"; break;
                case 22:
                    strContent = "二十二"; break;
                case 23:
                    strContent = "二十三"; break;
                case 24:
                    strContent = "二十四"; break;
                case 25:
                    strContent = "二十五"; break;
                case 26:
                    strContent = "二十六"; break;
                case 27:
                    strContent = "二十七"; break;
                case 28:
                    strContent = "二十八"; break;
                case 29:
                    strContent = "二十九"; break;
                case 30:
                    strContent = "三十"; break;
                case 31:
                    strContent = "三十一"; break;
            }
            return strContent;
        }

    }
}
